package com.rainner.admin.server;

import java.sql.SQLException;
import java.util.List;

import com.rainner.admin.javabean.ManagerBean;
import com.rainner.admin.util.dbutil.BaseService;
import com.rainner.admin.util.sha256.ShaUtil;

public class ManagerServer extends BaseService<ManagerBean> {
	/**
	 * 返回操作员详细信息
	 * 
	 * @param rows
	 *            行数
	 * @param page
	 *            页码
	 * @return
	 * @throws SQLException
	 */
	public List<ManagerBean> manageData(long rows, long page) throws SQLException {
		String sqlSelect = "select * from Manager limit ?,?";
		return super.query(sqlSelect, (page - 1) * rows, rows);
	}

	/**
	 * 返回操作员总数量
	 * 
	 * @return
	 * @throws SQLException
	 */
	public long manageDataCount() throws SQLException {
		String sqlSelect = "select COUNT(1) from Manager";
		return super.counts(sqlSelect);
	}

	/**
	 * 根据用户名删除操作员
	 * 
	 * @param username
	 * @return
	 * @throws SQLException
	 */
	public boolean manageDelData(String username) throws SQLException {
		String selSelect = "select Count(1) from Manager where ManagerName = ?";
		if (super.counts(selSelect, username) < 1) {
			return false;
		} else {
			String sqlDelete = "delete  from Manager where ManagerName = ?";
			return super.update(sqlDelete, username);
		}
	}

	/**
	 * 新增操作员
	 * 
	 * @param username
	 * @param password
	 * @param remark
	 * @return
	 * @throws Exception
	 */
	public boolean manageAddData(String username, String password, String remark) throws Exception {
		password = ShaUtil.toSha256String(password);
		String selSelect = "select Count(1) from Manager where ManagerName = ?";
		if (super.counts(selSelect, username) > 0) {
			return false;
		} else {
			String sqlAdd = "insert into Manager(ManagerName,ManagerPassword,ManagerRemark) values(?,?,?)";
			return super.update(sqlAdd, username, password, remark);
		}
	}

	/**
	 * 判断用户输入密码是否正确
	 * 
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public boolean checkManage(String username, String password) throws Exception {
		password = ShaUtil.toSha256String(password);
		String selSelect = "select Count(1) from Manager where ManagerName = ? and ManagerPassword = ?";
		return (super.counts(selSelect, username, password) > 0);
	}

	/**
	 * 修改操作员密码
	 * @param username
	 * @param password
	 * @return
	 * @throws SQLException
	 * @throws Exception
	 */
	public boolean updataPassword(String username, String password) throws SQLException, Exception {
		password = ShaUtil.toSha256String(password);
		String selSelect = "update Manager set ManagerPassword = ? where ManagerName = ?";
		return super.update(selSelect, password,username);
	}
}
